package com.lx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class RestfulController {

    //原来请求地址：http://localhost:8899/calculateNumCommon?a=1&b=665
    @RequestMapping("calculateNumCommon")
    public String common(int a, int b, Model model){
        model.addAttribute("msg",a + "+" + b + "=" + (a+b));
        return "hello";
    }

    //Restful风格请求地址：http://localhost:8899/calculateNumRestful/3/3
    //指定get方式请求
    // @RequestMapping(value = "calculateNumRestful/{a}/{b}",method = RequestMethod.GET)
    //可以简写
    @GetMapping("calculateNumRestful/{a}/{b}")
    public String restful(@PathVariable int a, @PathVariable int b, Model model){
        model.addAttribute("msg",a + "+" + b + "=（get请求）" + (a+b));
        return "hello";
    }

    //此时两个请求路径相同，但是调用方是get只会走get的方法，post只会走post方法
    //http://localhost:8899/calculateNumRestful/3/3
    @PostMapping("calculateNumRestful/{a}/{b}")
    public String restfulPost(@PathVariable int a, @PathVariable int b, Model model){
        model.addAttribute("msg",a + "+" + b + "=（post请求）" + (a+b));
        return "hello";
    }
}
